<template>
  <el-dialog :title="title" :visible.sync="Visible" width="70%" :before-close="handleClose">
    <el-form :model="formData" status-icon ref="formData" label-width="100px">
      <el-form-item label="标题">
        <el-input v-model="formData.title" readonly></el-input>
      </el-form-item>
      <el-form-item label="标签">
        <!-- props.multiple = true来开启多选模式 
      emitPath  选中节点改变时，是否返回由该节点所在的各级
      菜单的值所组成的数组，若设置 false，则只返回该
        节点的值  选择后的格式是[[1,2]] v-model绑定的格式需要[1,2]  为了匹配后期发请求 所以emitPath: false-->
        <el-cascader
          style="display:block"
          :options="labelOptions"
          v-model="formData.labelList"
          :props="{ multiple: true, emitPath: false, children: 'labelList', value: 'id',label: 'name'}"
          clearable
        ></el-cascader>
      </el-form-item>
      <el-form-item label="主图">
        <img :src="formData.imageUrl" style="width:178px;height:178px;display:block" />
      </el-form-item>
      <el-form-item label="是否公开">
        <el-radio-group v-model="formData.ispublic" :disabled="true">
          <el-radio :label="1">公开</el-radio>
          <el-radio :label="0">不公开</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="简介">
        <el-input type="textarea" v-model="formData.summary" readonly></el-input>
      </el-form-item>
      <el-form-item label="内容">
        <!-- mavonEditor -->
        <mavonEditor :editable="false" v-model="formData.mdContent"></mavonEditor>
      </el-form-item>
      <el-form-item align="center" v-if="isAudit">
        <el-button type="primary" @click="auditSuccess('formData')">审核通过</el-button>
        <el-button type="danger" @click="auditSFail()">审核不通过</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import article from "@/api/article";
// / 1. 引入 mavon-editor 组件与样式
import { mavonEditor } from "mavon-editor";
import "mavon-editor/dist/css/index.css";
export default {
  components: { mavonEditor },
  props: {
    title: { type: String, default: "" },
    isAudit: true, //是否为审核界面
    id: null, //文章id
    Visible: {
      //监听界面弹出显示
      type: Boolean,
      default: false
    },
    remoteClose: Function
  },
  data() {
    return {
      formData: {},
      labelOptions: []
    };
  },

  watch: {
    Visible(val) {
      if (val) {
        this.fetchData();
        this.getLabelOptions();
      } 
    }
  },
  methods: {
    fetchData() {
      article.getById(this.id).then(res => {
        this.formData = res.data;
        console.log(res, "res");
      });
    },
    // 获取标签
    getLabelOptions() {
      article.getCategoryAndLabel().then(res => {
        console.log(res, "getCategoryAndLabel");
        this.labelOptions = res.data;
      });
    },
    //   关闭弹窗
    handleClose() {
      this.remoteClose();
    },
    // 审核通过
    auditSuccess() {
      this.$confirm("此操作将确认审核通过, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          article.auditSuccess(this.id).then(res => {
            this.remoteClose();
            console.log(res);
          });
          this.$message({
            type: "success",
            message: "通过审核成功!"
          });
        })
        .catch(() => {
         
        });
    },
    // 审核不通过
    auditSFail() {
      this.$confirm("此操作将确认审核不通过, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
          article.auditFail(this.id).then(res => {
            console.log(res);
          });
          this.$message({
            type: "success",
            message: "不通过审核成功!"
          });
        }).catch(() => {
           
        });
    }
  }
};
</script>